package com.neuedu.javase.io;

import java.io.*;

/**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-10-05 8:48
 * 描述 : 字节的输入输出
 */
public class MyByteStream {

    public static void main(String[] args) throws Exception {

//        readOneByte();


//        readByteMore();

        //读取所有字节
//        readAllByte();


        /**
         * 输出字节
         */
//        write();

        
        //文件的复制
        String src = "d:\\io\\a.txt";
        String dest = "d:\\io\\b.txt";
        boolean success = copy(src,dest);
        System.out.println("success = " + success);


    }




    /**
     * 读取文件内容  读一个字节
     */
    static void  readOneByte(){

        File file = new File("D:\\io\\a.txt");
        InputStream is = null;
        //ALT +ENTER 提示
        try {
            is = new FileInputStream(file);

            int oneByte = is.read();

            System.out.println("oneByte = " + (char)oneByte);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }

    }



    /**
     * 读取文件内容  读多个字节
     */
    static void  readByteMore(){

        File file = new File("D:\\io\\a.txt");
        InputStream is = null;
        //ALT +ENTER 提示
        try {
            is = new FileInputStream(file);

            byte[] bts = new byte[5];

            //每次尽量读5个字节
            int len = is.read(bts);

            System.out.println("len = " + len);
            System.out.println("bts = " + new String(bts));


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }

    }



    /**
     * 读取文件内容  读所有字节
     */
    static void  readAllByte(){

        File file = new File("D:\\io\\a.txt");
        InputStream is = null;
        //ALT +ENTER 提示
        try {
            is = new FileInputStream(file);

            byte[] bts = new byte[5];

//            while(true) {
//                //每次尽量读5个字节
//                int len = is.read(bts);
//
//                System.out.println("len = " + len);
//                System.out.println("bts = " + new String(bts));
//
//                if(len == -1){
//                    break;
//                }
//            }

            int len = -1;
            while(  (len = is.read(bts) ) != -1 ) {
                System.out.println("len = " + len);
                System.out.println("bts = " + new String(bts ,0 ,len));
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }

    }


    static void write(){
        String fileName  = "D:\\io\\a.txt";
        OutputStream os = null;
        try {
//            os = new FileOutputStream(fileName); //默认是覆盖
            os = new FileOutputStream(fileName,true); //追加写入


            //写入 65代表的字符  A
//            os.write(65);

            //写入一个字节数组
//            byte[] bts = new byte[]{66,67,68,69,70};
//            byte[] bts = "Hello World".getBytes();
//            os.write(bts);

            byte[] bts2 = "\r\nnewline---------------end".getBytes();
            os.write(bts2,0,9);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }



    static boolean copy(String src ,String dest) throws Exception {
        return copy(new File(src) ,new File(dest));
    }

    static boolean copy(File src ,File dest) throws Exception {

        if(!src.exists()){
            throw new Exception("["+src.getPath()+"]文件不存在");
        }

        InputStream is = null;
        OutputStream os = null;


        try{



            //读写内容
            is = new FileInputStream(src);
            os = new BufferedOutputStream(new FileOutputStream(dest));
            byte[] bts = new byte[10];

            int len = -1;
            while(  (len = is.read(bts) ) != -1 ) {
//              bts

                os.write(bts ,0,len);
                os.flush();//刷新缓冲区
            }


            return true;
        }catch(Exception e){

        }finally {
           //关闭资源
            if(is != null){
                is.close();
            }
            if(os != null){
                os.close();
            }

        }



        return false;

    }







}
